<?php

/**
 * staticpage actions.
 *
 * @package    MVM Vehicle Showroom (r) showroom
 * @subpackage staticpage
 * @author     Bo Wang
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class staticpageActions extends sfActions {
  /**
   * Executes index action
   *
   * @param sfRequest $request A request object
   */
  public function executeIndex(sfWebRequest $request) {

    $this->page = Doctrine::getTable('Page')->findOneBySlug($request->getParameter('slug'));

    $this->forward404Unless($this->page,'Requested page does not exist!');

    $this->getResponse()->setTitle($this->page->getTitle().' @ '. sfConfig::get('app_company_name'));

    if($request->getParameter('slug')=='contact_us') {
      $this->form = new ContactUsForm();
      if($request->isMethod('post')) {
        $this->processForm($request, $this->form);
      }
    }
  }
  private function processForm(sfWebRequest $request, sfForm $form) {

    $requestData = $request->getPostParameters();

    if (sfConfig::get('app_recaptcha_active', false)) {
      $requestData['challenge'] = $this->getRequestParameter('recaptcha_challenge_field');
      $requestData['response'] = $this->getRequestParameter('recaptcha_response_field');
    }
    $this->form->bind($requestData);
    if ($this->form->isValid()) {

      $to = sfConfig::get('app_enquiry_email_account');

      $message = $this->getMailer()->compose(
              array($request->getParameter('email') => $request->getParameter('name')),
              $to['email'],
              'Contact Us Form Enquiry @ '.sfConfig::get('app_company_name').' '.date('d-m-Y'),
<<<EOF
Name: {$request->getParameter('name')}
Email: {$request->getParameter('email')}
Phone: {$request->getParameter('phone')}

Enquiry:
              {$request->getParameter('body')}

EOF
      );

      $this->getMailer()->send($message);
      
	  $this->getUser()->setFlash('notice', 'Your enquiry has been sent!');
	  $this->redirect('staticpage/index?slug=contact_us');
    }

  }
}
